package com.riaevolution.components
{
	import mx.containers.Canvas;
	import mx.events.ItemClickEvent;
	import mx.modules.IModuleInfo;
	import mx.core.UIComponent;
	import mx.events.ModuleEvent;
	import flash.events.Event;
	import mx.modules.ModuleManager;
	import mx.managers.PopUpManager;
	import mx.effects.Parallel;
	import mx.effects.Move;
	import mx.effects.Resize;
	import mx.controls.ProgressBar;
	import flash.events.ProgressEvent;
	import mx.controls.ProgressBarMode;

	public class ModularCanvas extends Canvas
	{
		private var minfo:IModuleInfo;
		[Bindable]
		public var dataProvider:Object;
		public var recentIndex:int;
		private var __holder:UIComponent;
		private var _pb:ProgressBar;
		public function ModularCanvas()
		{
			//TODO: implement function
			super();
		}
		public function handleClick(event:ItemClickEvent):void {
			var ite:ItemClickEvent = event as ItemClickEvent;
			if(ite.item) {
				minfo = ModuleManager.getModule(ite.item.module);
				minfo.addEventListener(ModuleEvent.READY,handleModuleLoaded);
				minfo.addEventListener(ModuleEvent.ERROR,
					function (event:ModuleEvent):void {
						Alerter.error("Error al cargar el modulo\n"+event.errorText);
					}
				);
			}
			//if I haven't a component
			if(!ite.item.id){
				var coso:WindowPanel;
            	if(ite.item.popup) {
					coso = PopUpManager.createPopUp(holder,WindowPanel) as WindowPanel;
            		PopUpManager.centerPopUp(coso);
            	} else {
            		coso = new WindowPanel();
   				}
   				var pb:ProgressBar = this.addIndicator(coso as UIComponent);
   				minfo.addEventListener(ModuleEvent.PROGRESS,
   					function (event:ProgressEvent):void {
   						trace("cargando ... "+event.bytesLoaded+" - "+event.bytesTotal);
   						pb.setProgress(event.bytesLoaded, event.bytesTotal);
   					}
   				);
   				minfo.addEventListener(ModuleEvent.READY,
   					function (event:ModuleEvent):void {
   						//pb.label = "Ready!";
   						pb.parent.removeChild(pb);
   					}
   				);
            	coso.title = ite.item.label;
            	coso.titleIcon = ite.item.icon;
				coso.addEventListener(WindowPanel.MAXIMIZE,handleMaximize);
				coso.addEventListener(WindowPanel.MINIMIZE,handleMinimize);
				coso.addEventListener(WindowPanel.RESTORE,handleMaximize);
				coso.addEventListener(WindowPanel.CLOSE,handleClosePanel);
				ite.item.id = coso;
				if(!ite.item.popup) {
					UIComponent(holder).addChild(coso);
				}
				recentIndex = ite.index;
				coso.data = ite.index;
				coso.width = ite.item.width;
				coso.height = ite.item.height;
				minfo.load();
			}
			else {
				WindowPanel(ite.item.id).visible = true;
				WindowPanel(ite.item.id).bringTop();
			}
		}
		private function handleClosePanel(event:Event):void {
			dataProvider[event.target.data].id = null;
			holder.removeChild(WindowPanel(event.target));
		}
		private function handleModuleLoaded(event:ModuleEvent):void {
			var wp:WindowPanel = dataProvider[recentIndex].id as WindowPanel;
			var dda:UIComponent = minfo.factory.create() as UIComponent;
			wp.addChild(dda);
			dda.percentHeight = dda.percentWidth = 100;
		}
		public function set holder(ref:UIComponent):void {
			__holder = ref;
		}
		public function get holder():UIComponent {
			if(__holder == null) return this;
			return __holder;
		}
		
		private function handleMinimize(event:Event):void {
			var wp:WindowPanel = event.target as WindowPanel;
			wp.visible = false;
		}
		private function handleMaximize(event:Event):void {
			var wp:WindowPanel = event.target as WindowPanel;
			wp.visible = true;
			var p:Parallel = new Parallel();
			var m:Move = new Move(wp);
			var r:Resize = new Resize(wp);
			r.duration = 300;
			if(event.type == WindowPanel.RESTORE) {
				m.xTo = wp.originalX;
				m.yTo = wp.originalY;
				r.widthTo = wp.originalWidth;
				r.heightTo = wp.originalHeight;
			}
			else {
				m.xTo = 0;
				m.yTo = 0;
				r.widthTo = holder.width;
				r.heightTo = holder.height;
			}
			r.hideChildrenTargets = [wp];
			p.duration = 300;
			p.addChild(m);
			p.addChild(r);
			p.play();
		}
		private function addIndicator(ui:UIComponent):ProgressBar {
			var pb:ProgressBar = new ProgressBar();
			pb.mode = ProgressBarMode.MANUAL;
			return ui.addChild(pb) as ProgressBar;
		}
		private function showProgress(event:ModuleEvent):void {
			//_pb.setProgress(event.bytesLoaded,event.bytesTotal);
		}	
	}
}